﻿using System;
using System.Collections.Generic;
using System.Text;
using AMS.DataAccess.Trademark.BO;
using AMS.DataAccess.Common;
using AMS.DataAccess.Sql;
using AMS.DataAccess.Trademark.Interfaces;
using AMS.DataAccess.BO;

namespace AMS.DataAccess.Trademark
{
    public class CaseApplicantManager : ICaseApplicantManager
    {
        public bool AddCaseApplicant(CallContext context, CaseApplicantInfo caseApp)
        {
            SqlInsert insert = new SqlInsert("TT_Applicant");
            insert.AddColumn("CaseID", caseApp.CaseID);
            insert.AddColumn("ApplicantID", caseApp.ApplicantID);
            insert.AddColumn("IsFirst", caseApp.IsFirst);

            AMS.DataAccess.SqlDebugger.WriteLine(insert);

            return 0 < SqlDBHelper.ExecuteSql(insert.ToString());
        }

        public int BatchAddCaseApplicant(CallContext context, ICollection<CaseApplicantInfo> apps)
        {
            if (apps.Count == 0)
            {
                return 0;
            }

            SqlBatch batch = new SqlBatch();
            foreach (CaseApplicantInfo app in apps)
            {
                var insert = batch.CreateSqlInsert("TT_Applicant");
                insert.AddColumn("CaseID", app.CaseID);
                insert.AddColumn("ApplicantID", app.ApplicantID);
                insert.AddColumn("IsFirst", app.IsFirst);
            }

            System.Diagnostics.Debug.WriteLine(batch.ToString());

            return SqlDBHelper.ExecuteSql(batch.ToString());
        }

        public int BatchDeleteCaseApplicants(CallContext context, int caseId, ICollection<int> applicantIdList)
        {
            if (applicantIdList.Count == 0)
            {
                return 0;
            }

            SqlBatch batch = new SqlBatch();
            foreach (int appId in applicantIdList)
            {
                var delete = batch.CreateSqlDelete("TT_Applicant");
                delete.AddCondition(new SqlCondition("CaseID", caseId));
                delete.AddCondition(new SqlCondition("ApplicantID", appId));
            }

            System.Diagnostics.Debug.WriteLine(batch.ToString());

            return SqlDBHelper.ExecuteSql(batch.ToString());
        }

        public bool DeleteCaseApplicant(CallContext context, int caseId, int applicantID)
        {
            SqlDelete delete = new SqlDelete("TT_Applicant");
            delete.AddCondition(new SqlCondition("CaseID", caseId));
            delete.AddCondition(new SqlCondition("ApplicantID", applicantID));

            AMS.DataAccess.SqlDebugger.WriteLine(delete);

            return 0 < SqlDBHelper.ExecuteSql(delete.ToString());
        }

        public List<CaseApplicantInfo> GetCaseApplicants(CallContext context, int caseId)
        {
            return GetCaseApplicants(context, caseId, false);
        }

        public List<CaseApplicantInfo> GetCaseApplicants(CallContext context, int caseId, bool queryFirstOnly)
        {
            return GetCaseApplicants(context, caseId, 0, queryFirstOnly);
        }

        public CaseApplicantInfo GetCaseApplicant(CallContext context, int caseId, int appId)
        {
            List<CaseApplicantInfo> apps = GetCaseApplicants(context, caseId, appId, false);
            if (apps != null && apps.Count > 0)
            {
                return apps[0];
            }
            else
            {
                return null;
            }
        }

        private List<CaseApplicantInfo> GetCaseApplicants(CallContext context, int caseId, int appId, bool queryFirstOnly)
        {
            SqlBuilder sql = new SqlBuilder();
            sql.Add("SELECT c.*,");
            sql.Add("    a.IsFirst");
            sql.Add("FROM TT_Applicant a");
            sql.Add("INNER JOIN TC_Customer c ON a.ApplicantID = c.AID");
            sql.Add("WHERE ", new SqlCondition("CaseID", caseId));
            if (queryFirstOnly)
            {
                sql.Add("    AND a.IsFirst = 1");
            }

            if (appId != 0)
            {
                sql.Add("    AND ", new SqlCondition("c.AID", appId));
            }

            AMS.DataAccess.SqlDebugger.WriteLine(sql);

            var ds = SqlDBHelper.Query(sql.ToString());
            return BusinessObjectMapping.DataTableToBOList<CaseApplicantInfo>(ds.Tables[0]);
        }
    }
}
